package coms.dataaccess.hibernate;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import coms.exception.HibernateAccessLayerException;

public abstract class AbstractDAO {
	 
	private Session session;

	private Transaction transaction;
	
    public void AbstractDao() {
        HibernateFactory.buildIfNeeded();
    }
    
    protected void saveOrUpdate(Object obj) {
        try {
            startOperation();
            session.saveOrUpdate(obj);
            transaction.commit();
        } catch (HibernateException e) {
            handleException(e);
        } finally {
            HibernateFactory.close(session);
        }
    }
    
    protected void delete(Object obj) {
        try {
            startOperation();
            session.delete(obj);
            transaction.commit();
        } catch (HibernateException e) {
            //handleException(e);
        } finally {
            HibernateFactory.close(session);
        }
    }
    
    protected Object find(Class clazz, String id) {
        Object obj = null;
        try {
            startOperation();
            obj = session.get(clazz, id);
            transaction.commit();
        } catch (HibernateException e) {
            handleException(e);
        } finally {
            HibernateFactory.close(session);

        }
        return obj;
    }
    
    protected Object find(Class clazz, Integer id) {
        Object obj = null;
        try {
            startOperation();
            obj = session.get(clazz, id);
            //transaction.commit();
        } catch (HibernateException e) {
            handleException(e);
        } finally {
            HibernateFactory.close(session);

        }
        return obj;
    }
    
    protected List findAll(Class clazz) {

		        /**
		         * WORKSHOP HIBERNATE STARTS HERE
		         *
		         * 1. Initialise a List called objects.
		         * 2. Call method startOperation for getting a hibernate Session Object
		         *          (You can have a look at the details of the method at HibernateUtil class in current package)
		         * 3. Create a Query object and create a query from session using clause from and clazz.getName()
		         * 4. Fire list command on created query
		         * 5. Commit the transaction variable tx
		         * 6. Make sure you handle exceptions properly
		         * 7. close HibernateFactory
		         * 8. return the objects which resulted from list command
         		 */
        List  objects = null;
        try {
            startOperation();
            Query query = session.createQuery("from " + clazz.getName());
            objects = query.list();
            //transaction.commit();
        } catch (HibernateException e) {
            handleException(e);
        } finally {
            HibernateFactory.close(session);
        }
        return objects;
    }

    protected List  findByCategory(Class clazz, String cat, String param) {
        List objects = null;
        try {
            startOperation();
            String hql = "from " + clazz.getName() + " c where c." + cat.trim()
            + "= '" + param.trim() + "'";
            Query query = session.createQuery(hql);
            objects = query.list();
            transaction.commit();
        } catch (HibernateException e) {
            handleException(e);
        } finally {
            HibernateFactory.close(session);
        }
        return objects;
    }
    
    protected List  manyToManyfindById(Class clazz, String containList, String id, String param) {
        List objects = null;
        try {
            startOperation();
            String hql = "from " + clazz.getName() + " c join c."+ containList.trim()+ " r where r." + id.trim()
            + "= '" + param.trim() + "'";
            Query query = session.createQuery(hql);
            objects = query.list();
            //transaction.commit();
        } catch (HibernateException e) {
            handleException(e);
        } finally {
            HibernateFactory.close(session);
        }
        return objects;
    }
    
   

    
    protected List findTransaction(int ConferenceItemID, int userID){
    	List objects = null;
    	try {
            startOperation();
            String hql = "from TransactionDTO t where t.userID=:UserID and t.conferenceItemID=:ConferenceITemID " +
            		"and paymentStatus='success'";
    		Query query = session.createQuery(hql);
    		query.setParameter("UserID", userID);
    		query.setParameter("ConferenceITemID", ConferenceItemID);
            objects = query.list();
        } catch (HibernateException e) {
            handleException(e);
        } finally {
            HibernateFactory.close(session);
        }
        return objects;  	
    }
    
    protected void handleException(HibernateException e)
    throws HibernateAccessLayerException {
        HibernateFactory.rollback(transaction);
        throw new HibernateAccessLayerException(e);
    }

    
    protected void startOperation() throws HibernateException {
        session = HibernateFactory.openSession();
        transaction = session.beginTransaction();
    }

	public List findAssociatedStatus(int uID) {
		// TODO Auto-generated method stub
		List objects = null;
    	try {
            startOperation();
            String hql = "from ConferenceItemDTO c join c.listOfParticipants cu join cu.userDTO u" +
            		" where u.userID = :uID";// and cu.status='Eligible'";
            Query query = session.createQuery(hql);
    		query.setParameter("uID", uID);
            objects = query.list();
            //transaction.commit();
        } catch (HibernateException e) {
            handleException(e);
        } finally {
            HibernateFactory.close(session);
        }
        return objects; 
	}

}
